home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12165 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to simulate EOF from the keyboard in Visual C++ 4.0, CTRL+Z doesn't work well
  5. Date: 18 Mar 1996 17:54:38 GMT
  6. Organization: Netcom
  7. Message-ID: <4ik80u$14k@reader2.ix.netcom.com>
  8. References: <314CB927.22A3@cco.caltech.edu>
  9. NNTP-Posting-Host: den-co8-14.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Mon Mar 18  9:54:38 AM PST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <314CB927.22A3@cco.caltech.edu>, xuhua@cco.caltech.edu says...
  16. >
  17. >The title says everything. When I try to simulate the EOF from the 
  18. >keyboard in Visual C++ with CTRL+Z, it doesn't work. CTRL+Z just kill 
  19. >the running program. I am running windows 95.
  20. >Please try the following simple program from <<C++ Primer Plus>>:
  21. >
  22. >Program #1
  23. >// textin3.cpp -- reading chars to end of file
  24. >#include <iostream.h>
  25. >int main(void)
  26. >{
  27. >        char ch;
  28. >        int count = 0;
  29. >
  30. >        while (cin.get(ch))      // cin.get(ch) is 0 on EOF
  31. >        {
  32. >                cout << ch;
  33. >                count++;
  34. >        }
  35. >        cout << count << " characters read\n";
  36. >        return 0;
  37. >}
  38. >
  39.  
  40. When I do this with Viaul C++, hitting CTRL-Z does end the program
  41. without flushing the output.  Even adding cout.flush() does not
  42. improve matters.  However, if you add logic to end the loop for
  43. some other reason, such as encountering a 'q' key, it is fine,
  44. which leads me to believe that the DOS emulation in either VC++
  45. or Windows95 is closing cout when the EOF is encountered for cin.
  46.  
  47. When this is compiled and run using Borland 4.5 it works
  48. (i.e., prints the count), so it sounds like a bug in the VC++
  49. cin/cout command-line emulation.
  50.  
  51. john lilley
  52.  
  53.